package tsg.component;

import java.util.ArrayList;
import java.util.HashMap;

import tsg.graphic.Graphics;
import tsg.layout.LayoutManager;
import tsg.physics.Collision;
import tsg.shape.Rectangle;
import tsg.util.Point;

public class Container extends Component
{	
	/**
	 * 
	 * @param layoutManager
	 */
	public Container(LayoutManager layoutManager)
	{
		_components = new ArrayList<Component>();
		
		this.setLayoutManager(layoutManager);
	}
	
	
	/**
	 * 
	 * @param component
	 */
	public void add(Component component)
	{
		_components.add(component);
	}
	
	/**
	 * 
	 * @param component
	 */
	public void remove(Component component)
	{
		_components.remove(component);
	}
	
	
	/**
	 * 
	 */
	public void removeAll()
	{
		_components.clear();
	}
	
	/**
	 * 
	 * @param component
	 * @param constraint
	 */
	public void add(Component component, Object constraint)
	{
		if(_constraints == null)
		{
			_constraints = new HashMap<Component, Object>();
		}
		
		_constraints.put(component, constraint);
		this.getComponents().add(component);
	}
	
	/**
	 * 
	 * @return
	 */
	public LayoutManager getLayoutManager()
	{
		return _layoutManager;
	}
	
	/**
	 * 
	 * @param layoutManager
	 */
	public void setLayoutManager(LayoutManager layoutManager)
	{
		_layoutManager = layoutManager;
	}
	
	
	/**
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public Component getComponentAt(int x, int y)
	{
		Component result = null;
		
		if(super.contains(x, y) == true)
		{
			for(int i=0; i<_components.size() && result == null; i++)
			{
				result = _components.get(i).getComponentAt(x, y);
			}
			
			if(result == null)
			{
				result = this;
			}
		}
		
		return result;
	}
	
	
	
	
	/**
	 * 
	 * @return
	 */
	public final ArrayList<Component> getComponents()
	{
		return _components;
	}
	
	
	/**
	 * @param g
	 */
	public void paint(Graphics g)
	{
		super.paint(g);
		
		Rectangle containerRectangle = new Rectangle(super.getPosition(), super.getSize());
		Rectangle componentRectangle = null;
		Component currentComponent   = null;
		
		containerRectangle.setPosition(new Point(0, 0));
		
		for(int i=0; i<_components.size(); i++)
		{
			componentRectangle = new Rectangle(_components.get(i).getPosition(), _components.get(i).getSize());
			
			// Only paint sub components if they lie within our rectangle
			if(Collision.rectanglesIntersect(containerRectangle, componentRectangle) == true)
			{
				currentComponent = _components.get(i);
				currentComponent.triggerLayoutManager();
				g.translate(currentComponent.getPosition().getX(), currentComponent.getPosition().getY());
				currentComponent.paint(g);
				g.translate(currentComponent.getPosition().getX()*-1, currentComponent.getPosition().getY()*-1);
			}
		}
	}
	
	/**
	 * 
	 */
	public void triggerLayoutManager()
	{
		_layoutManager.layoutContainer(this);
	}
	
	/**
	 * 
	 * @param component
	 * @return Object
	 */
	public Object getConstraint(Component component)
	{
		return _constraints.get(component);
	}

	private ArrayList<Component> 		_components 	= null;
	private LayoutManager 				_layoutManager 	= null;
	private	HashMap<Component, Object> 	_constraints 	= null;
}
